package com.yiqi.lottery.feature.documentary.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.suozhang.framework.entity.bo.PageData;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.util.RxKeyboardTool;
import com.yiqi.lottery.entity.bo.HallBo;
import com.yiqi.lottery.feature.documentary.contract.DocumentaryContract;
import com.yiqi.lottery.feature.documentary.dependencies.hall.DaggerDocumentaryComponent;
import com.yiqi.lottery.feature.documentary.dependencies.hall.DocumentaryPresenterModule;
import com.yiqi.lottery.feature.documentary.view.adapter.DocumentaryAdapter;
import com.yiqi.lottery.feature.documentary.view.adapter.LotteryDetailActivity;

import javax.inject.Inject;

public class SearchActivity extends BaseActivity implements DocumentaryContract.View, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edt_search)
    EditText mEdtSearch;
    @BindView(R.id.rv_search)
    RecyclerView mRvSearch;
    @Inject
    DocumentaryContract.Presenter mPresenter;
    private DocumentaryAdapter mAdapter;

    private EmptyView mEmptyView;
    PageData<HallBo> mData;
    private int pageSize = 10;
    private int mCurrentCounter = 0;
    private int pageIndex = 0;
    private boolean isErr;
    private boolean mLoadMoreEndGone = false;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_search;
    }


    @Override
    protected void initInjector() {
        DaggerDocumentaryComponent.builder().documentaryPresenterModule(new DocumentaryPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "跟单搜索", true, true);
        initAdapter();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        mEdtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {


            @Override
            public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                // TODO Auto-generated method stub
                if (arg1 == EditorInfo.IME_ACTION_SEARCH) {
                    pageIndex = 1;
                    mPresenter.getHallList(mEdtSearch.getText().toString(), pageIndex, null, 1);
                    RxKeyboardTool.hideSoftInput(SearchActivity.this);
                }
                return false;
            }

        });
    }

    private void initAdapter() {
        mAdapter = new DocumentaryAdapter();
        mEmptyView = new EmptyView(mRvSearch);
        mRvSearch.setLayoutManager(new LinearLayoutManager(this));
        mAdapter.bindToRecyclerView(mRvSearch);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemChildClickListener(this);
        mAdapter.setOnLoadMoreListener(this);

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(this, LotteryDetailActivity.class);
        intent.putExtra("id", mData.getList().get(position).getOrder_id());
        startActivity(intent);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(this, PersonalInfoActivity.class);
        intent.putExtra("id", mData.getList().get(position).getUser_info().getUser_id());
        startActivity(intent);
    }

    @Override
    public void showHallListSuccess(PageData<HallBo> hallBoPageData) {

        mData = hallBoPageData;
        mAdapter.setNewData(mData.getList());
        if (mAdapter.getData().size() < pageSize) {
            mAdapter.loadMoreEnd();
        }
        isErr = false;
        mCurrentCounter = pageSize;
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showHallListError(String ex) {

        mAdapter.setEmptyView(mEmptyView.getErrorView(ex));
        mAdapter.setNewData(null);
    }

    @Override
    public void showHallListMoreSuccess(PageData<HallBo> hallBoPageData) {
        isErr = false;
        mAdapter.addData(hallBoPageData.getList());
        mCurrentCounter = mAdapter.getData().size();
        mAdapter.loadMoreComplete();
    }

    @Override
    public void showHallListMoreError(String ex) {
        isErr = true;
        mAdapter.loadMoreFail();
    }

    @Override
    public void showHallListEmpty() {
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
        mAdapter.setNewData(null);
    }

    @Override
    public void onLoadMoreRequested() {

        if (mAdapter.getData().size() < pageSize) {
            mAdapter.loadMoreEnd(true);
        } else {

            if (mCurrentCounter >= mData.getTotal()) {
                mAdapter.loadMoreEnd(mLoadMoreEndGone);
            } else {
                if (!isErr) {
                    pageIndex++;
                    mPresenter.getHallMoreList(mEdtSearch.getText().toString(), pageIndex, null, null);

                }
            }

        }
    }
}
